[Ruby] Why do I have to URI.encode even safe characters for Net::HTTP requests?

Posted by Matthias on Stack Overflow See other posts from Stack Overflow or by Matthias
Published on 2010-03-11T09:30:31Z Indexed on 2010/03/16 15:31 UTC
Read the original article Hit count: 384

Filed under:
|
|

I was trying to send a GET request to Twitter (user ID replaced for privacy reasons) using Net::HTTP:

url = URI.parse("http://api.twitter.com/1/friends/ids.json?user_id=12345")
resp = Net::HTTP.get_response(url)

this throws an exception in Net::HTTP:

NoMethodError: undefined method empty?' for #<URI::HTTP:0x59f5c04> from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1470:ininitialize'

just by coincidence, I stumbled upon a similar code snippet, which used URI.encode prior to URI.parse, so I copied that and tried again:

url = URI.parse(URI.encode("http://api.twitter.com/1/friends/ids.json?user_id=12345"))
resp = Net::HTTP.get_response(url)

now it works fine, but why? There are no reserved characters that need escaping in the URL I mentioned, so why do I have to call URI.encode for get_response to succeed?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about http